<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="城市" prop="city" label-width="100px">
        <el-select
          v-model="queryParams.city"
          placeholder="请选择城市"
          style="width: 240px"
          clearable
        >
          <el-option
            v-for="item in cityOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户编号" prop="consNo" label-width="100px">
        <el-input
          v-model="queryParams.consNo"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="consName" label-width="100px">
        <el-input
          v-model="queryParams.consName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="市场化属性" prop="schsx" label-width="100px">
        <el-select
          v-model="queryParams.schsx"
          placeholder="请选择市场化属性"
          style="width: 240px"
          clearable
        >
          <el-option
            v-for="item in schsxOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用电类别" prop="ydlb" label-width="100px">
        <el-select
          v-model="queryParams.ydlb"
          placeholder="请选择用电类别"
          style="width: 240px"
          clearable
        >
          <el-option
            v-for="item in ydlbOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="签约公司" prop="qygs" label-width="100px">
        <el-input
          v-model="queryParams.qygs"
          placeholder="请输入签约公司"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="总电量" prop="totaldl" label-width="100px">
        <el-input
          v-model="queryParams.totaldlStart"
          placeholder="请输入总电量起始值"
          clearable
          class="!w-240px"
        />
        　至　
        <el-input
          v-model="queryParams.totaldlEnd"
          placeholder="请输入总电量结束值"
          clearable
          class="!w-240px"
        />
        <el-input
          v-show="false"
          v-model="queryParams.totaldl"
          placeholder="请输入总电量起始值"
          clearable
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['knowledge:elec-data-ln:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['knowledge:elec-data-ln:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
      show-summary
      :summary-method="sumMethod"
    >
      <el-table-column type="selection" width="55" />
      <!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="城市" align="center" prop="city" />
      <!--      <el-table-column label="供电单位" align="center" prop="org" />-->
      <!--      <el-table-column label="客户编号" align="center" prop="custNo" />-->
      <el-table-column label="用户编号" align="center" prop="consNo" />
      <el-table-column label="客户名称" align="center" prop="consName" />
      <el-table-column label="签约公司" align="center" prop="qygs" />
      <el-table-column label="签约价格" align="center" prop="qyjg" />
      <el-table-column label="市场化属性" align="center" prop="schsx" />
      <el-table-column label="总电量" align="center" prop="totaldl" />
      <el-table-column label="用电地址" align="center" prop="address" />
      <el-table-column label="合同容量" align="center" prop="htrl" />
      <el-table-column label="运行容量" align="center" prop="yxrl" />
      <!--      <el-table-column label="城乡标志" align="center" prop="cxbz" />-->
      <el-table-column label="行业类别" align="center" prop="hylb" />
<!--      <el-table-column label="客户分类" align="center" prop="hkfl" />-->
      <!--      <el-table-column label="临时电标志" align="center" prop="lsdbz" />-->

      <el-table-column label="用电类别" align="center" prop="ydlb" />
      <el-table-column label="用户电压" align="center" prop="dhdy" />
<!--      <el-table-column label="用户状态" align="center" prop="status" />-->
      <el-table-column label="202409电量" align="center" prop="dl202409" />
<!--      <el-table-column label="202409电费" align="center" prop="df202409" />-->
      <el-table-column label="202410电量" align="center" prop="dl202410" />
<!--      <el-table-column label="202410电费" align="center" prop="df202410" />-->
      <el-table-column label="202411电量" align="center" prop="dl202411" />
<!--      <el-table-column label="202411电费" align="center" prop="df202411" />-->
      <el-table-column label="202412电量" align="center" prop="dl202412" />
<!--      <el-table-column label="202412电费" align="center" prop="df202412" />-->
      <el-table-column label="202501电量" align="center" prop="dl202501" />
<!--      <el-table-column label="202501电费" align="center" prop="df202501" />-->
      <el-table-column label="202502电量" align="center" prop="dl202502" />
<!--      <el-table-column label="202503电费" align="center" prop="df202502" />-->
      <el-table-column label="202503电量" align="center" prop="dl202503" />
<!--      <el-table-column label="202503电费" align="center" prop="df202503" />-->
      <el-table-column label="202504电量" align="center" prop="dl202504" />
<!--      <el-table-column label="202504电费" align="center" prop="df202504" />-->
      <el-table-column label="202505电量" align="center" prop="dl202505" />
<!--      <el-table-column label="202505电费" align="center" prop="df202505" />-->
      <el-table-column label="202506电量" align="center" prop="dl202506" />
<!--      <el-table-column label="202506电费" align="center" prop="df202506" />-->
      <el-table-column label="202507电量" align="center" prop="dl202507" />
<!--      <el-table-column label="202507电费" align="center" prop="df202507" />-->
      <el-table-column label="202508电量" align="center" prop="dl202508" />
<!--      <el-table-column label="202508电费" align="center" prop="df202508" />-->
<!--      <el-table-column label="总电费" align="center" prop="totaldf" />-->
      <el-table-column label="法人联系人电话" align="center" prop="frlxdh" />
      <el-table-column label="法人联系人" align="center" prop="frlxr" />
      <el-table-column label="经办人电话" align="center" prop="jbrdh" />
      <el-table-column label="经办人" align="center" prop="jbr" />
      <el-table-column label="账务联系人电话" align="center" prop="cwlxdh" />
      <el-table-column label="账务联系人" align="center" prop="cwlxr" />
      <el-table-column label="电气联系人电话" align="center" prop="dqlxdh" />
      <el-table-column label="电气联系人" align="center" prop="dqlxr" />
      <el-table-column label="停送电联系人电话" align="center" prop="tsdlxdh" />
      <el-table-column label="停送电联系人" align="center" prop="tsdlxr" />

<!--      <el-table-column label="操作" align="center" min-width="120px">-->
<!--        <template #default="scope">-->
<!--          <el-button-->
<!--            link-->
<!--            type="primary"-->
<!--            @click="openForm('update', scope.row.id)"-->
<!--            v-hasPermi="['knowledge:elec-data-ln:update']"-->
<!--          >-->
<!--            编辑-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            link-->
<!--            type="danger"-->
<!--            @click="handleDelete(scope.row.id)"-->
<!--            v-hasPermi="['knowledge:elec-data-ln:delete']"-->
<!--          >-->
<!--            删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ElecDataLnForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ElecDataLnApi, ElecDataLn } from '@/api/knowledge/elecdataln'
import ElecDataLnForm from './ElecDataLnForm.vue'

/** 用电信息查询 列表 */
defineOptions({ name: 'ElecDataLn' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ElecDataLn[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  city: undefined,
  consNo: undefined,
  consName: undefined,
  hylb: undefined,
  schsx: undefined,
  ydlb: undefined,
  dhdy: undefined,
  totaldl: [],
  createTime: [],
  totaldlStart: undefined,
  totaldlEnd: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const ydlbOptions = [
  {
    value: '大工业用电',
    label: '大工业用电'
  },
  {
    value: '普通工业',
    label: '普通工业'
  },
  {
    value: '非工业',
    label: '非工业'
  },
  {
    value: '商业用电',
    label: '商业用电'
  },
  {
    value: '大工业其它优待',
    label: '大工业其它优待'
  },
  {
    value: '非居民照明',
    label: '非居民照明'
  },
  {
    value: '其他居民生活用电',
    label: '其他居民生活用电'
  },
  {
    value: '农业排灌',
    label: '农业排灌'
  },
  {
    value: '乡村居民生活用电',
    label: '乡村居民生活用电'
  },
  {
    value: '城镇居民生活用电',
    label: '城镇居民生活用电'
  },
  {
    value: '学校教学用电',
    label: '学校教学用电'
  }
]
const schsxOptions = [
  {
    value: '市场化零售客户',
    label: '市场化零售客户'
  },
  {
    value: '普通代购客户',
    label: '普通代购客户'
  },
  {
    value: '非市场化客户',
    label: '非市场化客户'
  }
]
const cityOptions = [
  {
    value: '沈阳',
    label: '沈阳'
  },
  {
    value: '大连',
    label: '大连'
  },
  {
    value: '鞍山',
    label: '鞍山'
  },
  {
    value: '抚顺',
    label: '抚顺'
  },
  {
    value: '本溪',
    label: '本溪'
  },
  {
    value: '丹东',
    label: '丹东'
  },

  {
    value: '锦州',
    label: '锦州'
  },
  {
    value: '营口',
    label: '营口'
  },
  {
    value: '盘锦',
    label: '盘锦'
  },
  {
    value: '阜新',
    label: '阜新'
  },
  {
    value: '辽阳',
    label: '辽阳'
  },

  {
    value: '铁岭',
    label: '铁岭'
  },
  {
    value: '葫芦岛',
    label: '葫芦岛'
  },
  {
    value: '朝阳',
    label: '朝阳'
  }
]
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ElecDataLnApi.getElecDataLnPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  queryParams.totaldl = []
  queryParams.totaldl.push(queryParams.totaldlStart)
  queryParams.totaldl.push(queryParams.totaldlEnd)

  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.totaldlStart = undefined
  queryParams.totaldlEnd = undefined
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ElecDataLnApi.deleteElecDataLn(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除用电信息查询 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await ElecDataLnApi.deleteElecDataLnList(checkedIds.value)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: ElecDataLn[]) => {
  checkedIds.value = records.map((item) => item.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ElecDataLnApi.exportElecDataLn(queryParams)
    download.excel(data, '用电信息查询.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})

// 自定义合计方法
const sumMethod = ({ columns, data }) => {
  const sums = []

  columns.forEach((column, index) => {
    if (index === 3) {
      // 第一列显示"合计"
      sums[index] = '合计'
      return
    }

    if (column.property === 'totaldl') {
      // 列计算总和
      const values = data.map((item) => Number(item[column.property]))
      sums[index] = values.reduce((acc, curr) => acc + curr, 0)

      // 添加单位/格式化显示
      sums[index] += ' '
    } else {
      // 非数值列留空
      sums[index] = ''
    }
  })

  return sums
}
</script>
